package uia.alumni.web;

import java.io.PrintWriter;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;

import uia.alumni.data.User;
import uia.alumni.form.*;

import static uia.alumni.form.FieldType.*;
import static uia.alumni.web.Constants.*;

/**
 * 
 * This Servlet is a Demonstration of the Form Validation. 
 * @author Simon Zimmermann.
 */
public class FormTest extends Page  {
    public static final String SERVLET_NAME = "web.form";
    public final static long serialVersionUID = 1;

    @PersistenceUnit(name=PERSISTENCE_UNIT)
    private EntityManagerFactory emf;

    public FormTest(){
        super(null);
    }

   /**
    * Write the body HTML for the Form test
    * @param out
    * @param request
    */
   public void writeBody(PrintWriter out, HttpServletRequest request) {

        out.println("<h1>Form Field Test.</h1>");
        out.print("<form action="+FormTest.SERVLET_NAME+".do method=\"GET\">");
        out.print("<input type=\"text\" name="+ SEARCH +">");
        out.println("<input type=\"submit\" value=\"\">");
        out.println("</form>");

         try {
            Form form = new Form(request);
            /*FormField url = form.newField(SEARCH.toString(), URL);
            FormField text = form.newField(SEARCH.toString(), TEXT);*/
            FormField email = form.newField(SEARCH, EMAIL);
            /*FormField date = form.newField(SEARCH.toString(), DATE);
            FormField zip = form.newField(SEARCH.toString(), TEXT);*/

            /*
             * PRINT FORMS IF VALID. JUST DEMO
             */
            if (form.isValid()) {
                out.print("<strong>Form is valid</strong>" +
                        "<ul>" +
                        /*"   <li>" + url.getFieldValue() + "</li>" +
                        "   <li>" + text.getFieldValue() + "</li>" +*/
                        "   <li>" + email.getFieldValue() + "</li>" +
                        /*"   <li>" + date.getFieldValue() + "</li>" +
                        "   <li>" + zip.getFieldValue() + "</li>" +*/
                        "</ul>");
            }

            /*
             * ERROR HANDLING: WHAT WENT WRONG WITH THE FORM. Lets find out.
             */
            else {
                out.print("<p>Search Form is Invalid</p>");
                for (FormField ff : form.getFieldsList()){
                    out.print("<p>" + ff.getFieldType());
                        if(ff.isFieldValid()){
                            out.println(" - True");
                        }else{
                            out.println(" - <strong>False</strong>");
                        }
                }
            }

        } catch (Exception e) {
            out.println("<pre>");
            out.println(e.getMessage());
            out.println("</pre>");
        } finally {
            out.close();
        }
    }




}
